package com.xca.touchdemo.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatTextView;

public class MultiTouchView extends AppCompatTextView {
    private static final String TAG = "MultiTouchView" ;
    private float x1 , x2 , y1 , y2 ;

    public MultiTouchView(@NonNull Context context) {
        super(context);
    }

    public MultiTouchView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    @SuppressLint("ClickableViewAccessibility")
    @Override
    public boolean onTouchEvent(MotionEvent event){
        int index = event.getActionIndex() ;
        switch (event.getActionMasked()){
            case MotionEvent.ACTION_DOWN:
                Log.e(TAG, "第一个手指按下");

                break;
            case MotionEvent.ACTION_UP:
                Log.e(TAG, "最后一个手指抬起" );
                break;
            case MotionEvent.ACTION_MOVE:
                if (event.getPointerCount() == 2){
                    x2 = event.getX(1) ;
                    y2 = event.getY(1) ;

                    x1 = event.getX(0) ;
                    y1 = event.getY(0) ;
                    Log.d(TAG, "=======pointer======move"+"(" +
                            x1 + "," + y1 + "),(" + x2 + "," + y2 + ")");
                }
                break;
            case MotionEvent.ACTION_POINTER_DOWN:
                Log.e(TAG, "第"+(index+1) + "个手指按下" );
                break;
            case MotionEvent.ACTION_POINTER_UP:
                Log.e(TAG, "第"+(index+1) + "个手指抬起" );
                break;
        }
        return super.onTouchEvent(event) ;
    }
}
